package de.bazinga.rotw.objects.elevator;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;
import com.jme.math.Vector3f;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.BasicSyncManager;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;

public class ElevatorMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	private int elevatorIndex = 0;
	
	private Vector3f location = null;
	private int factor = 0;
	private boolean active = false;
	
	private boolean requestInfo = false;
	
	
	public static void handleMessage(ElevatorMessage emsg) {
		Elevator elevator = (Elevator)CollidableObjects.getInstance().getObjects("gameObjects").get(
				emsg.getElevatorIndex());
		
		if(!emsg.isRequestInfo()) {
			elevator.setLocalTranslation(emsg.getLocation());
			elevator.getElevatorController().setFactor(emsg.getFactor());
			elevator.getElevatorController().setActive(emsg.isActive());
		}else{
			// Just send the current data
			BasicSyncManager syncManager = 
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getSyncManager();
			
			ElevatorMessage updateMsg = new ElevatorMessage();
			updateMsg.setLocation(elevator.getLocalTranslation());
			updateMsg.setActive(elevator.getElevatorController().isActive());
			updateMsg.setFactor(elevator.getElevatorController().getFactor());
			updateMsg.setElevatorIndex(emsg.getElevatorIndex());
			
			if(syncManager instanceof Server) {
				((Server)syncManager).getServer().sendToPlayer(updateMsg, emsg.getPlayerId());
			}
		}
		
	}

	public void setElevatorIndex(int elevatorIndex) {
		this.elevatorIndex = elevatorIndex;
	}

	public int getElevatorIndex() {
		return elevatorIndex;
	}

	public void setLocation(Vector3f location) {
		this.location = location;
	}

	public Vector3f getLocation() {
		return location;
	}

	public void setFactor(int factor) {
		this.factor = factor;
	}

	public int getFactor() {
		return factor;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}
	

	public void setRequestInfo(boolean requestInfo) {
		this.requestInfo = requestInfo;
	}

	public boolean isRequestInfo() {
		return requestInfo;
	}

	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof ElevatorMessage)) return ;

		// Player touched a item
		ElevatorMessage message = (ElevatorMessage)msg;
		
		if(server) {
			ElevatorMessage.handleMessage(message);
			
			// Redirect to other clients
			if( !((ElevatorMessage)message).isRequestInfo()) {
				((Server)game.getSyncManager()).getServer()
					.sendToAllExcept((ElevatorMessage)message, message.getPlayerId());
			}
		}else{
			ElevatorMessage.handleMessage(message);
		}
	}
}